package com.comac.print.component.app.utils;

import com.alibaba.fastjson.JSONObject;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * @author wanghuanwei_502226
 * @date 2025/4/11
 */
@Component
@Slf4j
public class MinIOFileUtil {
    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.accessKey}")
    private String accessKeyId;
    @Value("${minio.secretKey}")
    private String accessKeySecret;
    @Value("${minio.bucket}")
    private String bucketName;

    private MinioClient minioClient;

    @PostConstruct
    public void initClient() {
        minioClient =  MinioClient.builder().endpoint(this.endpoint).credentials(this.accessKeyId, this.accessKeySecret).build();
    }
    
    public String uploadFile(MultipartFile file, String objectKey) {
        try {
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName)
                    .contentType("application/octet-stream")
                    .object(objectKey)
                    .stream(new ByteArrayInputStream(file.getBytes()), -1, 10485760)
                    .build());

            return endpoint + "/" + bucketName + objectKey;
        } catch (Exception e) {
            log.error("upload file {} to minio fail", objectKey, e);
        }
        return null;
    }
    
    public String uploadBytes(byte[] byteData, String objectKey) {
        try {
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName)
                    .contentType("application/octet-stream")
                    .object(objectKey)
                    .stream(new ByteArrayInputStream(byteData), -1, 10485760)
                    .build());

            return endpoint + "/" + bucketName + objectKey;
        } catch (Exception e) {
            log.error("upload file {} to minio fail", objectKey, e);
        }
        return null;
    }

    public JSONObject uploadInputStream(InputStream inputStream, String objectKey) {
        try {
            ObjectWriteResponse objectWriteResponse = minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName)
                    .contentType("application/octet-stream")
                    .object(objectKey)
                    .stream(inputStream, -1, 10485760)
                    .build());

            String printSheetOssUrl =  endpoint + "/" + bucketName + objectKey;
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("printSheetOssUrl", printSheetOssUrl);
            jsonObject.put("md5Code", objectWriteResponse.etag());
            return jsonObject;
        } catch (Exception e) {
            log.error("upload file {} to minio fail", objectKey, e);
        }
        return null;
    }
}
